 #! /bin/sh
## DO NOT ROOT!
if [ `whoami` = "root" ];then {
  echo "not for root users!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  echo "\033[1m[ENTER]\033[m" && read b
  exit
}
fi
if [ -f /usr/bin/pkexec ]; then {
  DIGITATETOROOT="pkexec"
  }
  else {
	if [ -f /usr/bin/kdesudo ]; then {
	  DIGITATETOROOT="kdesudo"
	}
	else {
	      if [ -f /usr/bin/gksu ]; then {
		DIGITATETOROOT="gksu"
	      }
	      else {
		    DIGITATETOROOT="sudo"
	      }
	      fi
	      }
	fi
      }
fi
  
  #Arch
  case `uname -m` in
  i686|i586|i486|i386)
  FARCH="i586" #for the downloadfilefile!
  JARCH="i386" #for the correct linking
  DMOD=-d32
  ;;
  amd64|x86_64)
  FARCH="x64" #for the downloadfilefile!
  JARCH="amd64" #for the correct linking
  DMOD=-d64
  ;;
  *)
  echo "Your arch \"`uname -m`\" is not supported by Oracle Java"
  echo "\033[1m[ENTER]\033[m" && read b
  exit
  ;;
esac


INSTDIR="${HOME}/.javainstall"
TMP="/tmp/OJAVA" ##DO NOT CHANGE THIS OR YOU WILL DELETE ALL OF YOUR FILES!!
VERSION="7u9"
WEBADRESS="http://www.oracle.com/technetwork/java/javase/downloads/jre7u9-downloads-1859586.html"
JRE="jre-${VERSION}-linux-${FARCH}.tar.gz"

##javastarter script
################################################################################
JASHLINE1="#! /bin/bash"
JASHLINE2="export J2D_PIXMAPS=\"server\""
JASHLINE3="export _JAVA_OPTIONS=\"-Xmx64M -Xms32M\""
JASHLINE4="\"\`dirname \$0\`/java.bin\" ${DMOD} -client \$@"
JASHLINE5="exit"
################################################################################

##remove buggy java warning "banner" (top right element, causes javacrashes with kwin and grand access to the clipboard, most users just need it)
################################################################################
JAPOLINE1="grant {"
JAPOLINE2="permission java.awt.AWTPermission \"showWindowWithoutWarningBanner\";"
JAPOLINE3="permission java.awt.AWTPermission \"accessClipboard\";"
JAPOLINE4="};"
################################################################################

  
case $1 in
--install)
  case `lsb_release -is` in
  Ubuntu|ubuntu)
  echo -e "Hi! This is the installer for Oracle Java 7"
  echo "Ubuntu `lsb_release -rs` (`lsb_release -cs`) found. Install PPA"
    if [ -f /etc/apt/sources.list.d/webupd8team-java-`lsb_release -cs`.list ]; then {
      echo "Repository found, install Plugin"
      apturl apt://oracle-java7-installer?refresh=yes
      }
      else {
	#pkexec add-apt-repository ppa:webupd8team/java
	$DIGITATETOROOT add-apt-repository ppa:webupd8team/java
	apturl apt://oracle-java7-installer?refresh=yes
	}  
    fi
    ;;
  *)
    echo -e "Hi! This is the installer for Oracle Java 7"
    echo
    echo -e "you have to download:"
    echo -e "\033[1m${JRE}\033[m"
    echo -e "and save it in your downloads folder" 
    echo -e "\033[1m${HOME}/Downloads\033[m"
    
    ## Create this folder if there is none
    if [ ! -d "${HOME}/Downloads" ]; then {
      mkdir -p "${HOME}/Downloads"
      }
    fi

    ##start the Browser
    if [ ! -f "${HOME}/Downloads/${JRE}" ]; then {
    xdg-open ${WEBADRESS}
    }
    fi
    echo -e "\033[1m[ENTER]\033[m if you are ready"
    read b
    
    while [ ! -f "${HOME}/Downloads/${JRE}" ]
	do {
	    echo -e "${HOME}/Downloads/${JRE} not Found, Please download it and wait until the download is finished"
	    echo -e "\033[1m[ENTER]\033[m if you are ready"
	    read b
	  }
    done
	  
    ##create and move it to tmp
    mkdir -p ${TMP}
    mv ${HOME}/Downloads/${JRE} ${TMP}/ORACLE_JAVA.tar.gz
    
    ##check and extract tar
    if [ -f ${TMP}/ORACLE_JAVA.tar.gz ]; then {  
    tar xfv ${TMP}/ORACLE_JAVA.tar.gz --directory=${TMP}
    }
    else {
    rm -rf ${TMP}
    clear
    echo "something is wrong - exit (ERROR TAR)"
    echo "\033[1m[ENTER]\033[m" && read b
    exit
    }
    fi

    #rename
    mv ${TMP}/jre1.7.* ${TMP}/java-7-oracle

    ##enable the performance booster
    mv ${TMP}/java-7-oracle/bin/java ${TMP}/java-7-oracle/bin/java.bin
    echo ${JASHLINE1} > ${TMP}/java-7-oracle/bin/java
    echo ${JASHLINE2} >> ${TMP}/java-7-oracle/bin/java
    echo ${JASHLINE3} >> ${TMP}/java-7-oracle/bin/java
    echo ${JASHLINE4} >> ${TMP}/java-7-oracle/bin/java
    echo ${JASHLINE5} >> ${TMP}/java-7-oracle/bin/java
    chmod +x ${TMP}/java-7-oracle/bin/java
    
    ##Check for java.bin
  if [ ! -f "${TMP}/java-7-oracle/bin/java.bin" ]; then {  
  rm -rf ${TMP}
  clear
  echo "something is wrong - exit (ERROR JAVABIN)"
  echo "\033[1m[ENTER]\033[m" && read b
  exit
  }
  fi

  ##create INSTDIR and move folder
  mkdir -p ${INSTDIR}
  mv ${TMP}/java-7-oracle ${INSTDIR}
  cd ${INSTDIR}

  #clean up tmp
  rm -rf ${TMP}  ##DO NOT CHANGE THIS OR YOU WILL DELETE ALL OF YOUR FILES!!

  #check plugin
  if [ ! -f ${INSTDIR}/java-7-oracle/lib/${JARCH}/libnpjp2.so ]; then {  
  rm -rf ${INSTDIR}
  clear
  echo "something is wrong - exit (ERROR PLUGIN)"
  echo "\033[1m[ENTER]\033[m" && read b
  exit
  }
  fi

  ##create the plugin folder and link plugin
  mkdir -p ${HOME}/.mozilla/plugins
  ln -fs ${INSTDIR}/java-7-oracle/lib/${JARCH}/libnpjp2.so ${HOME}/.mozilla/plugins

  #check4 times :P for other plugins and thats all!
  clear
  if [ -f "/usr/lib/mozilla/plugins/libjavaplugin.so" ]; then {
  echo "last step - please uninstall \"icedtea-plugin\" or the browser will use this one"
  echo "\033[1m[ENTER]\033[m" && read n
  }
  fi
  if [ -f "/usr/lib/mozilla/plugins/libjavaplugin.so" ]; then {
  echo "you really should uninstall the \"icedtea-plugin\" in your Software Center"
  echo "\033[1m[ENTER]\033[m" && read n
  }
  fi
  if [ -f "/usr/lib/mozilla/plugins/libjavaplugin.so" ]; then {
  echo "this script is useless if you do not uninstall \"icedtea-plugin\"!"
  echo "\033[1m[ENTER]\033[m" && read n
  }
  fi
  if [ -f "/usr/lib/mozilla/plugins/libjavaplugin.so" ]; then {
  echo "Oracle Java is now installed for your Browser BUT \"icedtea-plugin\" is still there too!"
  }
  else {
  echo "Oracle Java is now installed for your Browser"
  }
  fi
  echo "\033[1m[ENTER]\033[m" && read b
  ;;  
  esac

  
  ##create java.policy
  echo ${JAPOLINE1} > ${HOME}/.java.policy
  echo ${JAPOLINE2} >> ${HOME}/.java.policy 
  echo ${JAPOLINE3} >> ${HOME}/.java.policy
  echo ${JAPOLINE4} >> ${HOME}/.java.policy
  
  exit
;;
--uninstall)
  echo -e "Uninstall the Oracle Java Installation? [yes]/[no]"
  echo
  read rf
    case ${rf} in
      yes|YES|Yes|Y|y)
      echo "YES"
      ;;
      *)
      echo "NO!"
      echo "\033[1m[ENTER]\033[m" && read b
      exit
      ;;
    esac

  rm -f ${HOME}/.mozilla/plugins/libnpjp2.so
  rm -fr ${INSTDIR}
  rm -f ${HOME}/.java.policy
  echo "uninstalled!"
  echo "\033[1m[ENTER]\033[m" && read b
;;
*)
  ##Some Terminal emulators can not handle this way. we try to use xterm in this case. 
  case ${DESKTOP_SESSION} in
    LXDE|lxde)
      if [ -d ${INSTDIR} ]; then {
	xterm -e sh -c "'${0}' --uninstall"
      }
      else {
	xterm -e sh -c "'${0}' --install"
      }
      fi
    ;;
    
    *)
      if [ -d ${INSTDIR} ]; then {
	x-terminal-emulator -e sh -c "'${0}' --uninstall"
      }
      else {
	x-terminal-emulator -e sh -c "'${0}' --install"
      }
      fi
    ;;
  esac
;;
esac
exit